home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / ansi / stdio / fflush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-25  |  793 b   |  40 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <libc/file.h>
  9.  
  10. int
  11. fflush(FILE *f)
  12. {
  13.   char *base;
  14.   int n, rn;
  15.  
  16.   if ((f->_flag&(_IONBF|_IOWRT))==_IOWRT
  17.       && (base = f->_base) != NULL
  18.       && (rn = n = f->_ptr - base) > 0)
  19.   {
  20.     f->_ptr = base;
  21.     f->_cnt = (f->_flag&(_IOLBF|_IONBF)) ? 0 : f->_bufsiz;
  22.     do {
  23.       n = write(fileno(f), base, rn);
  24.       if (n <= 0) {
  25.     f->_flag |= _IOERR;
  26.     return EOF;
  27.       }
  28.       rn -= n;
  29.       base += n;
  30.     } while (rn > 0);
  31.   }
  32.   if (f->_flag & _IORW)
  33.   {
  34.     f->_cnt = 0;
  35.     f->_flag &= ~(_IOWRT|_IOREAD);
  36.     f->_ptr = f->_base;
  37.   }
  38.   return 0;
  39. }
  40.